import 'package:lib_core/lib_core.dart';

class ErrorInterceptor extends QueuedInterceptorsWrapper {

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.statusCode == 200 && response.data is Map<String, dynamic>) {
      final code = response.data['code'];
      if (code == 'TKN0001' || code == 'TKN0003') {
        response.statusCode = 401;

        handler.reject(
          DioException(
            requestOptions: response.requestOptions,
            response: response,
            type: DioExceptionType.badResponse,
          ),
          true,
        );
        return;
      }
    }

    super.onResponse(response, handler);
  }

  /// 扫码激活绑定设备，激活失败的逻辑，上层自己自己处理，所以这里做个白名单过滤
  @override
  void onError(DioError err, ErrorInterceptorHandler handler) async {
    /*if (err.response != null) {
      // if (err.response!.statusCode == 401 && !_isAuthShowing && !_whileList.contains(err.requestOptions.path)) {
      //   _isAuthShowing = true;
      //   //登录失效
      //   BoostNavigator.instance.dialog(
      //     AuthInvalidDialog(
      //       onDismiss: () => _isAuthShowing = false,
      //     ),
      //     barrierDismissible: false,
      //   );
      // }
      err.response!.data = Rsp.createByDioError(err).toJson((value) => null);
      handler.resolve(err.response!);
    } else {
      handler.resolve(Response(
        requestOptions: err.requestOptions,
        data: Rsp.createByDioError(err).toJson((value) => null),
      ));
    }*/
  }
}
